home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / Chip_2004-07_cd1.bin / oddech / pipe / PipeDownDemo.exe / 2057 / SHOCKWAVE / 132 / 00012_[P] Skybox.ls < prev    next >
Encoding:
Text File  |  2004-05-25  |  4.6 KB  |  174 lines

  1. property pWorld, pCamera, pName, pScale, plFaces, pBuilt, plTextures, plShaders, pModelResource, pModel
  2.  
  3. on new me, tWorld, tName, tCamera
  4.   plFaces = [:]
  5.   plFaces[#front] = 0
  6.   plFaces[#back] = 0
  7.   plFaces[#left] = 0
  8.   plFaces[#right] = 0
  9.   plFaces[#up] = 0
  10.   plFaces[#down] = 0
  11.   pBuilt = 0
  12.   pName = tName
  13.   pWorld = member(tWorld)
  14.   pScale = 1.0
  15.   if voidp(tCamera) then
  16.     pCamera = 1
  17.   else
  18.     pCamera = tCamera
  19.   end if
  20.   return me
  21. end
  22.  
  23. on Destroy me
  24.   if not pBuilt then
  25.     return 0
  26.   end if
  27.   pWorld.deleteModel(pModel)
  28.   pWorld.deleteModelResource(pModelResource)
  29.   repeat with a in plShaders
  30.     pWorld.deleteShader(a)
  31.   end repeat
  32.   repeat with a in plTextures
  33.     pWorld.deleteTexture(a)
  34.   end repeat
  35.   pBuilt = 0
  36.   return 1
  37. end
  38.  
  39. on update me
  40.   if pBuilt then
  41.     pModel.transform.position = pWorld.camera[pCamera].transform.position
  42.     return 1
  43.   else
  44.     return 0
  45.   end if
  46. end
  47.  
  48. on SetFace me, tFace, tTexture, tlOrientation
  49.   if pBuilt then
  50.     return 0
  51.   end if
  52.   if [#front, #back, #left, #right, #up, #down].getOne(symbol(tFace)) = 0 then
  53.     return 0
  54.   end if
  55.   tm = member(tTexture)
  56.   tXOffset = 0.5 / tm.rect.width
  57.   tYOffset = 0.5 / tm.rect.height
  58.   tlUV = [[1.0 - tXOffset, 1.0 - tYOffset], [tXOffset, 1.0 - tYOffset], [tXOffset, tYOffset], [1.0 - tXOffset, tYOffset]]
  59.   if not listp(tlOrientation) then
  60.     tlOrientation = [tlOrientation]
  61.   end if
  62.   repeat with a in tlOrientation
  63.     case a of
  64.       #rotatecw:
  65.         tlUV.add(tlUV[1].duplicate())
  66.         tlUV.deleteAt(1)
  67.       #rotateccw:
  68.         tlUV.addAt(1, tlUV[4].duplicate())
  69.         tlUV.deleteAt(5)
  70.       #fliphorizontal:
  71.         tlUV = [tlUV[2], tlUV[1], tlUV[4], tlUV[3]]
  72.       #flipvertical:
  73.         tlUV = [tlUV[4], tlUV[3], tlUV[2], tlUV[1]]
  74.     end case
  75.   end repeat
  76.   plFaces[tFace] = [tm, tlUV]
  77.   return 1
  78. end
  79.  
  80. on SetSize me, tScale
  81.   if pBuilt or (tScale <= 0.0) then
  82.     return 0
  83.   end if
  84.   pScale = tScale
  85.   return 1
  86. end
  87.  
  88. on build me
  89.   if pBuilt then
  90.     return 0
  91.   end if
  92.   repeat with a = 1 to 6
  93.     if plFaces[a] = 0 then
  94.       return 0
  95.     end if
  96.   end repeat
  97.   plShaders = []
  98.   plTextures = []
  99.   repeat with a = 1 to 6
  100.     tFace = string(plFaces.getPropAt(a))
  101.     tm = plFaces[a][1]
  102.     tName = tm.name
  103.     tTextureFound = 0
  104.     tTextures = pWorld.texture.count
  105.     repeat with b = 1 to tTextures
  106.       if pWorld.texture[b].name = tName then
  107.         tTextureFound = 1
  108.       end if
  109.     end repeat
  110.     if tTextureFound then
  111.       tTexture = pWorld.texture(tName)
  112.     else
  113.       tTexture = pWorld.newTexture(tName, #fromCastMember, tm)
  114.       tTexture.renderFormat = #rgba8880
  115.       tTexture.nearFiltering = 1
  116.       tTexture.quality = #high
  117.     end if
  118.     plTextures.add(tTexture)
  119.     tShader = pWorld.newShader(pName && tFace && "'" & tName & "'", #standard)
  120.     tShader.texture = tTexture
  121.     tShader.textureMode = #none
  122.     tShader.textureRepeat = 0
  123.     tShader.emissive = rgb(255, 255, 255)
  124.     tShader.ambient = rgb(0, 0, 0)
  125.     tShader.diffuse = rgb(0, 0, 0)
  126.     tShader.specular = rgb(0, 0, 0)
  127.     plShaders.add(tShader)
  128.   end repeat
  129.   tlVertices = [vector(-pScale, -pScale, pScale), vector(pScale, -pScale, pScale), vector(pScale, -pScale, -pScale), vector(-pScale, -pScale, -pScale), vector(-pScale, pScale, pScale), vector(pScale, pScale, pScale), vector(pScale, pScale, -pScale), vector(-pScale, pScale, -pScale)]
  130.   tlTextureUVs = []
  131.   repeat with a = 1 to 6
  132.     repeat with b in plFaces[a][2]
  133.       tlTextureUVs.add(b.duplicate())
  134.     end repeat
  135.   end repeat
  136.   tlFaces = []
  137.   repeat with a = 1 to 6
  138.     case a of
  139.       1:
  140.         tlFaceVertices = [[7, 8, 3], [8, 4, 3]]
  141.       2:
  142.         tlFaceVertices = [[5, 6, 1], [6, 2, 1]]
  143.       3:
  144.         tlFaceVertices = [[8, 5, 4], [5, 1, 4]]
  145.       4:
  146.         tlFaceVertices = [[6, 7, 2], [7, 3, 2]]
  147.       5:
  148.         tlFaceVertices = [[6, 5, 7], [5, 8, 7]]
  149.       6:
  150.         tlFaceVertices = [[3, 4, 2], [4, 1, 2]]
  151.     end case
  152.     tUVOffset = (a - 1) * 4
  153.     tlFace = [plShaders[a], tlFaceVertices[1], [1, 2, 4] + tUVOffset]
  154.     tlFaces.add(tlFace.duplicate())
  155.     tlFace = [plShaders[a], tlFaceVertices[2], [2, 3, 4] + tUVOffset]
  156.     tlFaces.add(tlFace.duplicate())
  157.   end repeat
  158.   pModelResource = pWorld.newMesh(pName, 12, 8, 0, 0, 24)
  159.   pModelResource.vertexList = tlVertices
  160.   pModelResource.textureCoordinateList = tlTextureUVs
  161.   repeat with a = 1 to 12
  162.     tlFace = tlFaces[a]
  163.     pModelResource.face[a].shader = tlFace[1]
  164.     pModelResource.face[a].vertices = tlFace[2]
  165.     pModelResource.face[a].textureCoordinates = tlFace[3]
  166.   end repeat
  167.   pModelResource.generateNormals(#flat)
  168.   pModelResource.build()
  169.   pModel = pWorld.newModel(pName, pModelResource)
  170.   pModel.addToWorld()
  171.   pBuilt = 1
  172.   return 1
  173. end
  174.